// 出库记录添加所有产品
$("#outRecord").on("click",function () {
    //如果列表是收起的状态 就查询所有的产品
    if (!$(this).find(".arrow-line").is(".down")){
        var url = ctx + "api/traceOut/product/all"
        $.get(url,{},function (data) {
            var dd = $("#outRecord").next();
            dd.empty();
            $.each(data.data,function (index,val) {
                dd.append(`
                 <dl>
                     <div class="hot-area" onclick="getDatas(this)" id="${val.productId}">
                        <dt style="font-size: 15px" class="text-c f-18 fc-sub1 settledspan"> ${val.productName} </dt>
                        <div class="arrow-line"></div>
                     </div>
                     <dd class="hd"></dd>
                  </dl>`)
            })
        })
    }
})

//产品出库记录轮播图
function getDatas(s) {
    var hd = $(s).next(".hd")
        // .find(".swiper-wrapper");
    //清空滑块
    hd.empty()

    //使用Ajax获取分页数据并添加到滑块中去
    var productId = $(s).prop("id");
    var url = ctx + "api/traceOut/traceOut/list"
    var pageNumber = 1
    if(!$(s).find(".arrow-line").is(".down")){
        $.get(url,{productId:productId,page:pageNumber},function (res) {
            //定义滑块
            var swipers ="";
            var records = res.data.records;
            var total;
            //如果有数据，将数据添加到滑块中去
            if (records.length>0){
                /*var containers= '<div class="swiper-container"><div class="swiper-wrapper"></div>' +
                '<div class="swiper-pagination"></div>' +
                '<div class="swiper-button-next"></div>' +
                '<div class="swiper-button-prev"></div>' +
                '</div>';*/
                var containers  = $(`<div class="swiper-container">
                      <div class="swiper-wrapper"></div>
                      <div class="swiper-pagination"></div>
                      <div class="swiper-button-next"></div>
                      <div class="swiper-button-prev"></div>
                     </div>`)
                hd.append(containers)
                var wrapper = hd.find(".swiper-wrapper")
                total = res.data.total
                $.each(records,function (index,val) {
                    swipers+='<div class="swiper-slide"><div class="jg_text"><ul>' +
                        '<li>生产批号：'+val.batch+'</li>';
                    if (val.traceOutAttrFieldVOS.length>0){
                        $.each(val.traceOutAttrFieldVOS,function (i,v) {
                            swipers+='<li>'+v.attrFieldName+'：'+v.values+'</li>'
                        })
                    }
                    swipers+= '</ul></div></div>';
                })
                //再将滑块添加到容器中去
                wrapper.append(swipers)

                 let sw = new Swiper('.swiper-container', {
                    pagination: {
                        el: '.swiper-pagination',
                        type: 'fraction',
                        //设置分页总数
                        formatFractionTotal: function (num) {
                            if (total!=undefined){
                                return total;
                            }
                            return num;
                        }
                    },
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                    grabCursor : true,
                    observer: true,
                    observeParents: true,
                    on:{
                        reachEnd:function () { //滑动到最后一块时触发该方法
                            if (total>10){
                                if ((this.slides.length)>=total) {
                                    layer.msg("已加载完毕")
                                }else {
                                    pageNumber++;
                                    $.get(url,{productId:productId,page:pageNumber},function (resu) {
                                        swipers ="";
                                        records = resu.data.records;
                                        if (records.length>0){
                                            total = resu.data.total
                                            $.each(records,function (index,val) {
                                                swipers+='<div class="swiper-slide"><div class="jg_text"><ul>' +
                                                    '<li>生产批号：'+val.batch+'</li>';
                                                if (val.traceOutAttrFieldVOS.length>0){
                                                    $.each(val.traceOutAttrFieldVOS,function (i,v) {
                                                        swipers+='<li>'+v.attrFieldName+'：'+v.values+'</li>'
                                                    })
                                                }
                                                swipers+= '</ul></div></div>';
                                            })
                                            //再将滑块添加到容器中去
                                            sw.appendSlide(swipers)
                                        }
                                    })
                                }
                            }
                        }
                    }
                });

            }else{
                hd.append("<span>该产品尚未出库</span>")
            }


        });
    }

    //交替改变样式
    $(s).find(".arrow-line").toggleClass("down");
    //交替淡入淡出
    $(s).next(".hd").slideToggle();
}



/* 隐藏部分*/
$(function () {

    $(".leftbox").on("click", "div", function() {
        var $index = $(this).index();
        $(this).addClass('mmactive').siblings('div').removeClass('mmactive')
        $(".show_txt").eq($index).show().siblings(".show_txt").hide()

    })

    $(".hot-area").click(function() {
        //交替改变样式
        $(this).find(".arrow-line").toggleClass("down");
        //交替淡入淡出
        $(this).next(".hd").slideToggle();
    });

    /*// 监控
    var decoder;
    function init(url){
        decoder = new EZUIPlayer({
            id: 'playWind',
            autoplay: true,
            url: url,
            accessToken: accessToken,
            decoderPath: ctx+'js/ezuikitjs',
            width: 333,
            height: 300,
            splitBasis: 1, // 1*1 2*2 3*3 4*4
        });
    }

    //初始化 监控
    if (decoder!=undefined){
        decoder.stop();
    }else {
        init('ezopen://open.ys7.com/765865057/1.live');
    };
    //监控按钮实现
    $(".but").click(function () {
        if (decoder!=undefined){
            decoder.stop();
        }
        var url = $(this).val();
        if (url==null||url==''){
            layer.msg("该设备不存在");
        } else {
            init(url)
        }
    })*/

})





